package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbItemCatMapper;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.pojo.TbItemCatExample;
import com.dongdongshop.service.ItemCatService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@DubboService
public class ItemCatServiceImpl implements ItemCatService {

    @Resource
    private TbItemCatMapper tc;

    @Override
    public List<TbItemCat> getSelect(Long parentId) {
        TbItemCatExample example = new TbItemCatExample();
        example.createCriteria().andParentIdEqualTo(parentId);
        return tc.selectByExample(example);
    }

    @Override
    public int insert(TbItemCat c) {
        return tc.insertSelective(c);
    }

    @Override
    public TbItemCat getId(Long id) {
        return tc.selectByPrimaryKey(id);
    }

    @Override
    public int update(TbItemCat c) {
        return tc.updateByPrimaryKey(c);
    }

    @Override
    public TbItemCat getById(Long id) {
        return tc.selectByPrimaryKey(id);
    }

    @Override
    public TbItemCat updateId(Long id) {
        TbItemCat tbItemCat = tc.selectByPrimaryKey(id);
        Long parentId = tbItemCat.getParentId();
        if (parentId == 0) {
            return null;
        }
        return tc.selectByPrimaryKey(parentId);
    }

    @Override
    public int delete(Long id) {
        TbItemCat tbItemCat = tc.selectId(id);
        if(tbItemCat == null){
            tc.deleteByPrimaryKey(id);
        }
        return 1;
    }

    @Override
    public int deleteCheck(Long[] ids) {
        for (Long id : ids) {
            delete(id);
        }
        return 1;
    }


}
